require File.dirname(__FILE__) + '/test_helper.rb'

require 'rubygems'
require File.dirname(__FILE__) + '/../lib/oos4ruby.rb'

=begin
puts user.name
puts user.surname
puts user.no_mails
puts user.no_newsletter
puts user.only_contacts

user.no_mails = user.no_mails == 'true'?'false':'true'
user.no_newsletter = user.no_newsletter == 'true'?'false':'true'
user.only_contacts = user.only_contacts == 'true'?'false':'true'
user.update!

puts user.no_mails
puts user.no_newsletter
puts user.only_contacts

user.contacts.create! :slug => 'verbosemode', :trusted => true

contacts = user.contacts.refresh!
puts contacts.entries[0].to_xml

contact = contacts.entries[0]
contact.trusted = true
contact.update!

puts contact.to_xml

contact.delete!

contacts = user.contacts.refresh!
puts contacts.entries[0].to_xml

sites = oos.search({:q => 'tapas en madrid'})

puts sites.entries.to_s

sites = user.sites.entries


new_site = user.sites.create! :title => 'asdfw4rwqwasf', :user_address => 'asdfw4rwqwasf', 
  :country => {:name => 'España', :slug => '/es'}, :locality => {:name => 'Madrid', :slug => '/es/madrid'},
  :summary => 'nueva opinion desde la API', :review_content => 'asdfasdf',
  :tags => ['tapas', 'tipical spanish'], :lists => ['food']
  
puts new_site.to_xml || 'NOOOOOO'

user.sites.refresh!

site = user.sites.entries[0]

puts site.name
puts site.country_slug
puts site.locality_slug

site.tags.each { |tag| puts "TAG : #{tag.term}, #{tag.scheme} "}

site.add_tag('dfdfgfgggsdgdfgg')
site.update!

site.tags.each { |tag| puts "TAG : #{tag.term}, #{tag.scheme} "}

site.remove_tag( 'dfdfgfgggsdgdfgg' )
site.update!

site.tags.each { |tag| puts "TAG : #{tag.term}, #{tag.scheme} "}

file_path = '/home/david/media_api_test.png'

medias = site.multimedia

media = medias.create!(:file => file_path,
   :content_length => File.size(file_path), :content_type => 'image/png' )

puts media.to_s
site.multimedia.refresh!

puts site.multimedia.entries.to_s
=end

class TestOos4ruby < Test::Unit::TestCase

  def setup
    @oos = Oos4ruby::Oos.new
    @oos.auth_user('david.calavera@11870.com', '3beedf801db0d373919f6462345d6f76')
  end
  
  def assert_user_not_nil(user)
    assert_not_nil user, ''
  end
  
  def test_user_without_slug
    user = @oos.user
    assert_user_not_nil(user) 
  end
  
  def test_user_with_slug
    user = @oos.user('mamuso')
    assert_user_not_nil(user)   
  end
  
  def test_user_with_uri
    user = @oos.user("#{Oos4ruby::API_URL}/users/peralta")
    assert_user_not_nil(user)
  end
  
  def test_contacts
    user = @oos.user
    assert_not_nil user.contacts, ''
    assert (not user.contacts.empty?), ''
    
    contact = user.contacts[0]
    assert_not_nil contact, ''
    assert_not_nil contact.data, ''
    assert_not_nil contact.data.nick, ''
    assert_not_nil contact.data.slug, ''
  end
  
  def test_sites
    user = @oos.user
    assert_not_nil user.sites, ''
    assert_not_nil user.sites.size, ''
    assert_not_nil user.sites.total_size, ''
    assert_not_nil user.sites[0], ''
  end
  
  def test_all_sites
    user = @oos.user
    assert_not_nil user.sites.all, ''
    assert_equal user.sites.total_size, user.sites.all.size
  end
  
  def test_create_site
    site = @oos.user.sites.create! :title => 'asdfwdss4rwqwasf', :user_address => 'asdfw4rwqwasf', 
      :country => {:name => 'España', :slug => '/es'}, 
      :locality => {:name => 'Madrid', :slug => '/es/madrid'},
      :summary => 'nueva opinion desde la API', :review_content => 'asdfasdf',
      :tags => ['tapas', 'tipical spanish'], :lists => ['food'], :telephone => '913545471'
    
    assert_not_nil site
  end
  
  def test_contains?
    assert_equal(true, @oos.user.sites.contains?('oos:locality', 'Madrid'))
  end

end

